const express = require('express')
const router = express.Router()
const db = require('../mysql/index')

router.get('/userMain', (req, res) => {
    const sql = 'select username,nickname,phone,admin,picture from user where id=?'
    db.query(sql, req.auth.id, (err, results) => {
        if (err) {
            return res.send({ status: 1, msg: err.message })
        }
        if (results.length != 1) {
            return res.send({ status: 1, msg: '获取用户信息失败' })
        }
        const { id, password, ...userinfo } = results[0]
        res.send({ status: 0, msg: '获取用户信息成功', data: userinfo })
    })
})

router.post('/updateImg' , (req,res)=>{
    const img = req.body.picture
    const sql = `update user set picture=? where id=?`
    db.query(sql , [img, req.auth.id], (err,results)=>{
        if (err) {
            return res.send({ status: 1, msg: err.message })
        }
        res.send({ status: 0, msg: '更新用户头像成功'})
    })
})
module.exports = router